{aide_template 'common/header'}
<link href="{$aideUrl}web/resource/css/util.min.css" rel="stylesheet">
<script type="text/javascript" src="{$aideUrl}web/resource/js/util.min.js?v=20170912"></script>
<ul class="nav nav-tabs">
	<li {if $op == 'display'} class="active" {/if}><a href="{php echo web_url('activity/category/display');}">活动分类管理</a></li>
	<li {if $op == 'post' && empty($cateId) && $_GPC['visible_level']!=1}class="active"{/if}><a href="{php echo web_url('activity/category/post');}">添加分类</a></li>
	<!--<li {if $op == 'post' && empty($cateId) && $_GPC['visible_level']==1}class="active"{/if}><a href="{php echo web_url('activity/category/post',array('visible_level'=>1));}"><span style="color: red;">添加顶部分类</span></a></li>-->
	{if $op == 'post' && !empty($cateId)}
	<li class="active"><a href="{php echo web_url('activity/category/post');}">编辑活动分类</a></li>
	{/if}
</ul>
<div class="main">
{if $op == 'display'}
<style>
	td>i{cursor:pointer; display:inline-block; width:100%; height:100%; color:#428bca;}
	.category-caret{display:inline-block; width:20px; margin: 0 10px; text-align:center; cursor:pointer; color:#d9534f;}
	.add.add_level0{cursor:pointer;}
</style>
<form action="" method="post">
	<div class="panel panel-default">
		<div class="panel-heading clearfix">顺序号越大排列越靠前<span class="pull-right"><a href="javascript:;" class="js-category-all">全部折叠 </a></span></div>
		<div class="panel-body table-responsive">
			<table class="table table-hover">
				<thead>
					<tr>
						<th style="width:50px;">分类图片</th>
						<th style="width:auto">分类名称</th>
						<th style="width:100px; text-align:center;">开启状态</th>
						<th style="width:90px; text-align:center;">显示顺序</th>
						<!--<th style="width:60px; text-align:center;">类别</th>-->
						<th style="width:180px; text-align:right">操作</th>
					</tr>
				</thead>
				<tbody>
					{loop $category $pcate}
					<tr>
						<td>
							<img width='45' height="45" src="{php echo tomedia($pcate['thumb']);}" data-url="{php echo tomedia($pcate['thumb']);}" class="scrollLoading" style='float:left;' onerror="this.src='./resource/images/nopic-small.jpg'" />
						</td>
						<td class="text-left">
							<input type="text" class="form-control" maxlength="50" style='display:inline;width:150px;'name="name[{$pcate['id']}]" value="{$pcate['name']}" />
							{if empty($pcate['redirect'])}
                                <a href="{php echo web_url('activity/category/post', array('fatherid' => $pcate['id']))}" class="add add_level1" title="添加子分类" data-pid="{$pcate['id']}" data-name="{$pcate['name']}">
                                    <i class="fa fa-plus-circle"></i>添加子类
                                </a>
                                {if !empty($children[$pcate['id']])}
                                <span class="category-caret">
                                    <i class="fa fa-caret-down js-category-down" pid="{$pcate['id']}"></i>
                                    <i class="fa fa-caret-up js-category-up" style="display:none;" pid="{$pcate['id']}"></i>
                                </span>
                                {/if}
                            {/if}
                            {if !empty($pcate['redirect'])}<span class="label label-info">跳转网页</span>{/if}
                            {if $pcate['visible_level']==1}<span style="color:red;">首页显示</span>{/if}
						</td>
						<td class="text-center">
                            {if IMS_VERSION=='0.8'}
                            <input class="js-flag" type="checkbox" data-id="{$pcate['id']}" {if $pcate['enabled']}checked{/if}/>
                            {else}
                            <div class="switch switch{php echo $pcate['enabled'] ? 'On' : 'Off'}" data-id="{$pcate['id']}" style="margin:0 auto"></div>
                            {/if}
						</td>
						<td class="text-center">
							<input type="text" class="form-control"  name="displayorder[{$pcate['id']}]" value="{$pcate['displayorder']}" />
						</td>
						<!--<td style="text-align:center;display:none">
							{if $pcate['open']==2}自营{else}开放{/if}
						</td>-->
						<td class="text-right" style="position:relative;">
							<a href="javascript:;" data-id="{$pcate['id']}"  data-url="{php echo !empty($pcate['redirect'])?$pcate['redirect']:app_url('activity/activity/display',array('pid'=>$pcate['id']))}" id="js-copy{$pcate['id']}" class="js-copy js-clip">复制链接</a> -
							<a href="{php echo web_url('activity/category/post', array('id' => $pcate['id']));}" title="编辑">编辑</a> -
							<a href="javascript:;" cate-id="{$pcate['id']}" parentid="{$pcate['parentid']}" title="删除" class="js-delete">删除</a>
						</td>
					</tr>
					{loop $children[$pcate['id']] $ccate}
						<tr class="js-collpase js-child-category" pid="{$pcate['id']}" style="display:none;">
							<td>
								<img src="{php echo tomedia($ccate['thumb']);}" width='45' height="45" onerror="$(this).remove()" class="scrollLoading" style='float:left;' />
							</td>
							<td class="text-left">
								<div style="padding-left:50px;height:30px;line-height:30px;background:url('./resource/images/bg_repno.gif') no-repeat -245px -545px;">
									<input type="text" class="form-control" maxlength="50" style='display:inline;width:150px;' name="name[{$ccate['id']}]" value="{$ccate['name']}"/>
								</div>
							</td>
							<td class="text-center">
                                {if IMS_VERSION=='0.8'}
                                <input class="js-flag" type="checkbox" data-id="{$ccate['id']}" {if $ccate['enabled']}checked{/if}/>
                                {else}
                                <div class="switch switch{php echo $ccate['enabled'] ? 'On' : 'Off'}" data-id="{$ccate['id']}" style="margin:0 auto"></div>
                                {/if}
							</td>
							<td class="text-center">
								<input type="text" class="form-control" name="displayorder[{$ccate['id']}]" value="{$ccate['displayorder']}">
							</td>
							<!--<td></td>-->
							<td class="text-right" style="position:relative;">
								<a href="javascript:;" data-id="{$ccate['id']}"  data-url="{php echo app_url('activity/activity/display',array('pid'=>$pcate['id'],'cid' => $ccate['id']))}" id="js-copy{$ccate['id']}" class="js-copy js-clip">复制链接</a> -
								<a href="{php echo web_url('activity/category/post', array('id' => $ccate['id']));}" title="编辑">编辑</a> -
								<a href="javascript:;" cate-id="{$ccate['id']}" parentid="{$ccate['parentid']}" title="删除" class="js-delete">删除</a>
							</td>
						</tr>
					{/loop}
					{/loop}
					<!--<tr>
						<td colspan="4"><div class="add add_level0"><i class="fa fa-plus-circle"></i> 添加分类</div></td>
					</tr>-->
				</tbody>
			</table>
		</div>
	</div>
	<div class="form-group col-md-12">
		<input name="submit" type="submit" class="btn btn-primary min-width" value="保存">
		<input type="hidden" name="token" value="{$_W['token']}" />
	</div>
</form>
<script type="text/javascript">
{if IMS_VERSION=='0.8'}
require(['jquery', 'util'], function($, util){
	$('.js-copy').each(function(){
		var id=$(this).attr('data-id');
		util.clip($("#js-copy"+id), $(this).attr('data-url'));
	});
});
{/if}
//微擎0.8显示switch
require(['bootstrap.switch'],function($){
	// 启用状态
	$('.js-flag:checkbox').bootstrapSwitch({onText: '启用', offText: '禁用'});
	$('.js-flag:checkbox').on('switchChange.bootstrapSwitch', function(event, state) {
		var id = $(this).data('id');
		var status = state ? 1 : 0;
		$.post("{php echo web_url('activity/category/status')}", {id : id, status : status}, function (data){
				util.tips(data.message);
		}, 'json');
	});

});
//微擎1.0+显示switch
$('.switch').click(function(e){
	var id = $(this).data('id');
	var state = $(this).hasClass("switchOff");
	var status = state ? 1 : 0;
	$.getJSON("{php echo web_url('activity/category/status')}", {id : id, status : status}, function(data) {
		util.tips(data.message, 2000);
	});

	$(this).toggleClass("switchOff");
	$(this).toggleClass("switchOn");

});

$(function(){
	$('.js-clip').each(function(){
		util.clip(this, $(this).data('url'));
	});
	$('.js-delete').click(function(e) {
		e.stopPropagation();
		var cateid = $(this).attr('cate-id');
		var parentid = $(this).attr('parentid');
		var $this = $(this);
		util.nailConfirm(this, function(state) {
			if (!state) return;
			$.post("{php echo web_url('activity/category/delete')}", {cateid : cateid, parentid : parentid}, function(data) {
				if(!data.errno){
					$this.parent().parent().remove();
				};
				util.tips(data.message);
			}, 'json');
		});
	});
	//添加父分类
	$('.add_level0').click(function(){
		var pid = $(this).attr('data-pid');
		var name = $(this).attr('data-name');
		var html = '<tr>' +
				'<td></td><td class="text-left"> <input type="text" class="form-control" maxlength="50" style="display:inline;width:90%;" name="add_parent_name[]" value="" placeholder="分类名称" /></td><td></td>'+
				'<td class="text-center"> <input type="text" class="form-control" maxlength="2" name="add_parent_displayorder[]" value="0" placeholder="分类排序"/></td>' +
				'<td colspan="1"><i class="fa fa-times-circle" onclick="$(this).parents(\'tr\').remove()"></i></td>' +
				'</tr>';
		$(this).parents('tr').before(html)
	});
	//控制显示
	$('.js-category-all').click(function() {
		if($(this).hasClass('js-collapse')) {
			$('.js-child-category').fadeOut("slow");
			$('.fa-caret-up').hide();
			$('.fa-caret-down').show();
			$(this).text('全部展开').removeClass('js-collapse');

		} else {
			$('.js-child-category').fadeIn("slow");
			$('.fa-caret-up').show();
			$('.fa-caret-down').hide();
			$(this).text('全部折叠').addClass('js-collapse');
		}
	});

	$('.js-category-up').click(function() {
		var parentId = $(this).attr('pid');
		console.log(parentId);
		$('tr[pid="'+parentId+'"]').fadeOut("slow");
		$(this).prev().show();
		$(this).hide();
	});
	$('.js-category-down').click(function() {
		var parentId = $(this).attr('pid');
		console.log(parentId);
		$('tr[pid="'+parentId+'"]').fadeIn("slow");
		$(this).next().show();
		$(this).hide();
	});
});
</script>

{elseif $op == 'post'}

<div class="clearfix">
	<form action="" method="post" class="form-horizontal form" enctype="multipart/form-data" id="form1">
		<div class="panel panel-default" id="step1">
			<div class="panel-heading">
				分类管理
			</div>
			<div class="panel-body">
				{if !empty($parentid)}
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">上级分类</label>
					<div class="col-sm-9 col-xs-12 control-label" style="text-align:left;">{$parent['name']}</div>
				</div>
				{/if}
				<div class="form-group">
					<label class="col-md-2 control-label"><span style="color:red;">*</span> 分类名称：</label>
					<div class="col-md-9">
						<input class="form-control" type="text" maxlength="11" name="name" value="{$category['name']}" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">分类图片：</label>
					<div class="col-sm-9">
						{php echo tpl_form_field_image('thumb', $category['thumb'])}
                        <span class="help-block">建议尺寸：150*150 像素</span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">分类排序：</label>
					<div class="col-md-9">
						<input class="form-control" type="text" maxlength="2" name="displayorder" value="{if empty($category['displayorder'])}0{else}{$category['displayorder']}{/if}" />
						<span class="help-block">排序号越大排列越靠前。</span>
					</div>
				</div>
                {if !$faterId && !$category['parentid']}
                <div class="form-group">
					<label class="col-md-2 control-label">跳转连接：</label>
					<div class="col-md-9">
						<input class="form-control" type="text" name="redirect" value="{$category['redirect']}" />
						<span class="help-block">指定点击分类时要跳转的链接（注：链接需加http://；仅限一级分类）。</span>
					</div>
				</div>
                {/if}
                <div class="form-group">
                    <label class="col-md-2 control-label">设置颜色：</label>
                    <div class="col-sm-9">
                    	{php echo tpl_form_field_color('color', $value = $category['color'])}
                        <span class="help-block">颜色设置只支持一级分类，且在点击首页“全部”分类时才会生效</span>
                    </div>
                </div>
				<div class="form-group">
					<label class="col-md-2 control-label">首页推荐：</label>
					<div class="col-md-9">
						<label class="radio-inline">
							<input type="radio" name="visible_level" value="1" {if $category['visible_level'] == '1'} checked="checked" {/if} />开启
						</label>
						<label class="radio-inline">
							<input type="radio" name="visible_level" value="0" {if $category['visible_level'] == '0'} checked="checked" {/if} />关闭
						</label>
					</div>
				</div>
				<div class="form-group" style="display:none">
					<label class="col-md-2 control-label">是否自营：</label>
					<div class="col-md-9">
						<label class="radio-inline">
							<input type="radio" name="open" value="0" {if $category['open'] == '0'} checked="checked" {/if} />开放给多主办方
						</label>
						<label class="radio-inline">
							<input type="radio" name="open" value="1" {if $category['open'] == '1'} checked="checked" {/if} />自营
						</label>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label">是否开启：</label>
					<div class="col-md-9">
						<label class="radio-inline">
							<input type="radio" name="enabled" value="1" {if $category['enabled'] == '1'} checked="checked" {/if} />开启
						</label>
						<label class="radio-inline">
							<input type="radio" name="enabled" value="0" {if $category['enabled'] == '0'} checked="checked" {/if} />关闭
						</label>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-2 control-label"></label>
					<div class="col-md-9">
						<input name="submit" id="submit" type="submit" value="保存" class="btn btn-primary min-width">
						<input type="hidden" name="token" value="{$_W['token']}" />
					</div>
				</div>
			</div>
		</div>
	</form>
</div>
{/if}
</div>

{aide_template 'common/footer'}
